package com.customization.unionpay.market.workflowaction;

import com.alibaba.fastjson.JSONObject;
import com.customization.unionpay.market.maketEnum.InstructionEnum;
import com.customization.unionpay.market.util.ClearFundButtonRelated;
import com.customization.unionpay.market.util.FormDataUtils;
import com.customization.unionpay.market.util.PropUtils;
import weaver.generalledger.action.ZzgdExecuteInstrucitonAction;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.request.RequestManager;

import java.util.Map;

/**
 * @author sjm
 * @description 营销事项申请表单节点action
 */
public class MarketingApplicationChangeVerifyAction implements Action {

    private static final Logger log = LoggerFactory.getLogger(MarketingApplicationChangeVerifyAction.class);

    @Override
    public String execute(RequestInfo requestInfo) {

        RequestManager rm = requestInfo.getRequestManager();
        String requestid = String.valueOf(rm.getRequestid());
        try {
            // 流程表单id
            int formId = rm.getFormid();
            // 流程标题
            String requestName = requestInfo.getDescription();
            String tableName = rm.getBillTableName();          // 表单名称
            log.info("tableName:" + tableName);

            // 营销活动事项台账表单id
            int activityLedgerFormId = Integer.parseInt(com.customization.unionpay.market.util.PropUtils.BB_LOG.getPropValue("marketSubject", "marketing_activity_ledger_form_id"));
            String activityLedgerTableName = com.customization.unionpay.market.util.FormDataUtils.getTableName(activityLedgerFormId);

            com.customization.unionpay.market.workflowaction.MarketingApplicationChangeAction marketingApplicationChangeAction = new MarketingApplicationChangeAction();

            Map<String, Object> allFormData = com.customization.unionpay.market.util.FormDataUtils.getAllFormData(requestInfo);

            Map<String, String> mainMap = (Map<String, String>) allFormData.get("mainMap");
            // 所有明细表的值
            Map<String, Map<String, Map<String, String>>> detailMap = (Map<String, Map<String, Map<String, String>>>) allFormData.get("detailMap");

            // 主 业务类型        ywlx
            String ywlx = mainMap.get("ywlx");
            String ywlxName = com.customization.unionpay.market.util.FormDataUtils.getItemName(formId,"ywlx",ywlx);
            log.info("业务类型："+ywlxName);
            // 主 业务日期        ywrq
            String ywrq = mainMap.get("ywrq");
            log.info("ywrq：" + ywrq);
            // 主 预算控制期      yskzq
            String yskzq = mainMap.get("yskzq");
            log.info("yskzq：" + yskzq);

            String ywrqStr = com.customization.unionpay.market.util.FormDataUtils.changeDateString("yyyy-MM-dd", ywrq, "yyyy");
            String yskzqStr = com.customization.unionpay.market.util.FormDataUtils.changeDateString("yyyy-MM-dd", yskzq, "yyyy");

            String bdh = mainMap.get("bdh");
            log.info("bdh：" + bdh);
            if(bdh == null || "".equals(bdh)){
                bdh = "bdh";
            }

            String ysxsqdh = mainMap.get("ysxsqdh");
            log.info("ysxsqdh：" + ysxsqdh);

            String sqdh = com.customization.unionpay.market.util.FormDataUtils.selectMarketingActivityLedgerById(activityLedgerTableName, ysxsqdh);
            log.info("sqdh："+sqdh);
            // sfwdxy   是否为大协议
            String sfwdxy = mainMap.get("sfwdxy");
            String sfwdxyName = com.customization.unionpay.market.util.FormDataUtils.getItemName(formId, "sfwdxy", sfwdxy);
            log.info("是否为大协议："+sfwdxyName);
            // 预算 是否为大协议 =否
            if("否".equals(sfwdxyName)){
                JSONObject params = new JSONObject();

                String instruction = "";
                if("营销平台事项申请金额调增单".equals(ywlxName)){
                    log.info("提交预算营销平台事项申请金额调增单！");
                    if(ywrqStr.equals(yskzqStr)){
                        // 预算 业务日期的年份=预算控制期的年份  提交-占用
                        params = marketingApplicationChangeAction.getMarketingApplicationChangeAddMiddle(requestName,yskzq,yskzqStr, mainMap,detailMap,sqdh,bdh,tableName,requestid);
                        instruction = com.customization.unionpay.market.maketEnum.InstructionEnum.ZY.toString();
                    }else {
                        // 预算 业务日期的年份≠预算控制期的年份  提交-项目占用
                        params = marketingApplicationChangeAction.getMarketingApplicationChangeAddMiddle(requestName,yskzq,yskzqStr, mainMap,detailMap,sqdh,bdh,tableName,requestid);
                        instruction = InstructionEnum.XMZY.toString();
                    }
                    log.info("预算校验拼接参数："+params);
                    int yszjbFormId = Integer.parseInt(com.customization.unionpay.market.util.PropUtils.BB_LOG.getPropValue("marketSubject", "yszjb_form_id"));
                    int yszjbFormmodeId = Integer.parseInt(PropUtils.BB_LOG.getPropValue("marketSubject", "yszjb_formmode_id"));
                    String yszjbTableName = com.customization.unionpay.market.util.FormDataUtils.getTableName(yszjbFormId);
                    log.info("yszjbTableName：" + yszjbTableName);
                    String bId = FormDataUtils.addYuMiddleForm(yszjbFormId, yszjbFormmodeId, yszjbTableName, rm.getUser(), params);

                    log.info("instruction："+instruction);
                    ZzgdExecuteInstrucitonAction zzgdExecuteInstrucitonAction = new ZzgdExecuteInstrucitonAction();
                    zzgdExecuteInstrucitonAction.setZl(instruction);
                    String execute = zzgdExecuteInstrucitonAction.execute(requestInfo);
                    if("0".equals(execute)){
                        if(bId != null && !"".equals(bId)){
                            ClearFundButtonRelated.deleteByBillId(yszjbTableName, Integer.parseInt(bId));
                        }
                        String messageid = requestInfo.getRequestManager().getMessageid();
                        log.info("messageid："+messageid);
                        String messagecontent = requestInfo.getRequestManager().getMessagecontent();
                        log.info("messagecontent："+messagecontent);
                        return Action.FAILURE_AND_CONTINUE;
                    }else {
                        log.info("预算调用成功！");
                    }
                }
            }else {
                log.info("当前是否大协议不为否！");
            }
        } catch (Exception e) {
            // 异常报错是填写异常信息，阻止流程继续流转
            requestInfo.getRequestManager().setMessageid("10001");
            requestInfo.getRequestManager().setMessagecontent(e.getMessage());
            return Action.FAILURE_AND_CONTINUE;
        }
        return Action.SUCCESS;
    }

}
